package com.vue.controller;


import com.alibaba.fastjson.JSON;
import com.vue.bean.R;
import com.vue.entity.ShoppingCartEntity;
import com.vue.entity.po.ShoppingCartPO;
import com.vue.entity.vo.ShoppingCartVO;
import com.vue.service.IShoppingCartService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品购物车表 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-08-04
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    private IShoppingCartService shoppingCartService;

    @GetMapping("/selectList")
    public R selectList(ShoppingCartPO po){
        log.info("查询列表：{}", JSON.toJSON(po));
        if (po.getUserId() == null){
            return R.fail("请先登录");
        }
        try {
            List<ShoppingCartVO> list = shoppingCartService.selectList(po);
            return R.data(list);
        } catch (Exception e) {
            return R.fail("暂无数据");
        }
    }

    @GetMapping("/selectById")
    public R selectById(@RequestParam("id") Long id){
        log.info("查询业务字典详情：{}", id);
        try {
            return shoppingCartService.selectById(id);
        } catch (Exception e) {
            return R.fail("查询业务字典详情异常");
        }
    }

    @PostMapping("/addOrEditShoppingCart")
    public R addShoppingCart(@RequestBody ShoppingCartEntity po){
        log.info("新增业务字典：{}", JSON.toJSON(po));
        if (po.getUserId() == null){
            return R.fail("请先登录");
        }
        try {
            return shoppingCartService.addOrEditShoppingCart(po);
        } catch (Exception e) {
            return R.fail("新增异常");
        }
    }

    @GetMapping("/deleteById")
    public R deleteById(@RequestParam("id") Long id){
        log.info("删除已点菜品：{}", id);
        try {
            return shoppingCartService.deleteById(id);
        } catch (Exception e) {
            return R.fail("删除异常");
        }
    }

    @GetMapping("/saveToMy")
    public R saveToMy(Long userId){
        log.info("保存记录接口saveToMy");
        if (userId == null){
            return R.fail("请先登录");
        }
        try {
            return shoppingCartService.saveToMy(userId);
        } catch (Exception e) {
            return R.fail("保存记录异常");
        }
    }
}
